<template>
	<view class="activePage">
		<image class="topBanner" :src="$setting.imgUrl()+'/static/rongzi.jpg'" mode=""></image>
		<view class="bgActive">
			<view class="newCustomShop">
				<view class="product">
					<view class="product-list flex-space-between flex-wrap">
						<view class="product-item bg-fff" v-for="(item,index) in productList" :key="index"
							@click="jump('/pages/categoty/product/product_active?id='+item.product.product_id+'&activity_product_id='+item.activity_product_id)">
							<image class="product-item-image" :src="item.product.file_path"></image>
							<view class="product-con">
								<view class="product-con-name u-line-2">{{item.product.product_name}}</view>
								<!-- <view class="limitTip">限时直降</view> -->
								<view class="flex-space-between productTip">
									<view class="product-con-price">
										<view class="bigText"><text
												class="smallText">￥</text>{{sumIntMoney(item.activity_price)}}<text
												class="smallText">{{decimalPart(item.activity_price)}}</text>
										</view>
										<text class="throughText">￥{{makeSumThrough(item)}}</text>
									</view>
									<b-u-button-theme width="52rpx" height="40rpx" size="24rpx"
										@click="jump('/pages/categoty/product/product_active?id='+item.product.product_id+'&activity_product_id='+item.activity_product_id)">购买</b-u-button-theme>
								</view>
							</view>
						</view>

						<view class="product-item" v-if="productList.length % 2 == 1"></view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				productPageForm: {
					activity_id: 13
					// recommend: 0,
					// category_id: 0,
					// list_rows: 20,
				},
				productList: [],
				newCouponLists: [],
				userInfo: {},
			}
		},
		onShow() {
			this.getHttp();
		},
		methods: {
			makeSumThrough(item) {
				// 解析传入的参数 item
				const parsedItem = item;

				// 检查 line_price 是否为字符串 "0.00"
				if (parsedItem.line_price === "0.00" || parseFloat(parsedItem.line_price)<0.001) {
					// 如果是，则返回 sku 中所有对象的 line_price 的最大值
					const skuLinePrices = parsedItem.product.sku.map(sku => parseFloat(sku.line_price));
					const maxLinePrice = Math.max(...skuLinePrices);
					return maxLinePrice;
				} else {
					// 如果 line_price 不是 "0.00"，则直接返回它的值
					return parseFloat(parsedItem.line_price);
				}
			},
			decimalPart(product_price) {
				let price = parseFloat(product_price)
				// 使用 toFixed() 方法将价格保留两位小数，并将结果转换为字符串
				const priceWithDecimal = price.toFixed(2);

				// 使用 split() 方法将字符串按照 '.' 分割成两部分
				const parts = priceWithDecimal.split('.');

				// 返回小数部分的字符串
				return '.' + parts[1] || '00';
			},
			sumIntMoney(num) {
				return parseInt(num)
			},
			getHttp() {
				uni.$u.api.userInfo().then(res => {
					this.userInfo = res;

					this.getNewcomerCouponList();
					this.getProduct();
				})
			},
			postReceiveList(item) {
				if (item['is_receive'] == true) {
					uni.switchTab({
						url: '/pages/categoty/categoty'
					})
					return
				}
				console.log(this)
				if (uni.$u.test.isEmpty(this.userInfo.userInfo.mobile)) {
					this.jump("/pages/login/login");
					return;
				}
				let coupon_ids = [];
				this.newCouponLists.forEach((item, key) => {
					coupon_ids.push(item.coupon_id)
				})
				uni.$u.api.postReceiveList({
					coupon_ids
				}).then(res => {
					this.$emit('hideCouponModal', false)
					uni.showToast({
						title: "领取成功"
					})
					this.newCouponLists.map(item => {
						return item.is_receive = true
					})
				})
			},
			ling(coupon_id) {
				console.log('coupon_id', coupon_id)
				// let coupon_id = coupon_id;
				uni.$u.api.postReceiveOne({
					coupon_id
				}).then(res => {
					// this.$emit('hideCouponModal', false)
					uni.showToast({
						title: "领取成功"
					})
				})
			},
			getNewcomerCouponList() {
				uni.$u.api.getNewcomerCouponList({
					limit: 100
				}).then(res => {
					this.newCouponLists = res;
				})
			},
			getProduct() {
				uni.$u.api.getActivityProduct(this.productPageForm).then(res => {
					console.log(res, 'res')
					this.productList = res.list;
				})
			},
			jump(url) {
				uni.navigateTo({
					url: url
				})
			},
		},
		async onLoad(options) {},
	}
</script>

<style lang="scss">
	.activePage {
		display: flex;
		flex-direction: column;
		align-items: center;
		min-height: 100vh;

		.topBanner {
			width: 100%;
			height: 1100rpx;
		}


		.bgActive {
			width: 100vw;
			display: flex;
			justify-content: center;
			background-color: #fef7dd;
			// background: linear-gradient(180deg, #ffb4ac 0%, #efccc7 100%);
		}

		.newCustomShop {
			.product {
				margin-top: 20rpx;
				width: 692rpx;

				.xinrenjia {
					width: 33px;
					height: 12px;
				}

				.product-list {
					padding-bottom: 30rpx;
					margin-top: 28rpx;

					.productTip {
						padding: 0rpx 10rpx 0rpx 0rpx;
					}

					.product-item {
						width: 334rpx;
						border-radius: 16rpx;
						margin-bottom: 24rpx;
						overflow: hidden;
						// height: 72vw;
					}

					.product-item-image {
						width: 334rpx;
						height: 328rpx;
					}

					.product-con {
						padding: 12rpx 20rpx 18rpx;

						.product-con-name {
							font-size: 26rpx;
							color: #333;
							font-weight: bold;
							margin-bottom: 14rpx;
							min-height: 68rpx;
						}

						.limitTip {
							width: 120rpx;
							height: 36rpx;
							font-size: 23rpx;
							font-weight: normal;
							font-stretch: normal;
							line-height: 32rpx;
							letter-spacing: 0rpx;
							color: #ffffff;
							background: linear-gradient(90deg, #fd686f 0%, #ff934d 100%);
							display: flex;
							justify-content: center;
							align-items: center;
							padding: 10rpx 5rpx;
							border-radius: 10rpx;
							margin-bottom: 14rpx;
						}

						.product-con-price {
							.bigText {
								font-size: 35rpx;
								font-weight: normal;
								font-stretch: normal;
								line-height: 20rpx;
								color: #ff0000;




							}

							.throughText {
								text-decoration: line-through;
								color: #828282;
								font-size: 24rpx;
							}

							.smallText {
								font-size: 24rpx;
								font-weight: normal;
								color: #ff0000;
							}

							// view {
							// 	font-size: 28rpx;
							// 	font-weight: bold;
							// 	color: #fff;

							// 	text {
							// 		color: #fff;
							// 		font-size: 24rpx;
							// 		text-decoration: none;
							// 	}
							// }

							// text {
							// 	font-size: 22rpx;
							// 	text-decoration: line-through;
							// 	color: #fff;
							// }
						}
					}
				}
			}
		}
	}
</style>